<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">
$(function(){
	$("#role_func").attr("disabled" , false);
	var treeData;
	var uroleid='[#if Urole??]${Urole.id}[/#if]';
	//alert(uroleid);
	if(uroleid!=''){
		//如果是编辑
		$.ajax({
				url : '/ufunc/ajax_Functions.do',
				type : 'post',
				dataType : 'json',
				async :false,
				data : {
					roleId : uroleid
				},
				success : function(data){
					if(data.success){
						$.ajax({
							url : '/ufunc/ajax_authorityTree.do',
							async : false,
							type : 'post',
							dataType : 'json',
							data :{
								funcs : data.funcs
							},
							success : function(response){
								treeData = response;
							}
						});
						
						$("#functionTree").tree({
							data : treeData,
							checkbox : true,
							cascadeCheck : false
						});
					  
					}else{
						error_msg(data.msg , function(){location.reload();});
					}
				}
			});
	}else{
		//权限
		$.ajax({
			url : '/ufunc/ajax_authorityTree.do',
			async : false,
			type : 'post',
			dataType : 'json',
			success : function(data){
				treeData = data;
			}
		});

		$("#functionTree").tree({
			data : treeData,
			checkbox : true,
			cascadeCheck : false
		});
	}
	
})
//提交角色和权限
function subrole_func(obj){
	$(obj).attr("disabled" , true);
	var roleId = $("#createRole").find("input[name='id']").val();
	var deptId = $("#createRole").find("input[name='deptId']").val();
	var roleName = $.trim($("#createRole").find("input[name='roleName']").val());
	var roleType=$("#createRole").find("select[name='roleType']").val();
	var sortNum=$.trim($("#createRole").find("input[name='sortNum']").val());
	var code=$.trim($("#createRole").find("input[name='code']").val());
	var ifAdmin=$("#createRole").find("select[name='ifAdmin']").val();
	if(!roleName){
		art.dialog.tips("请填写角色名称");
		$(obj).attr("disabled" , false);
		return false;
	}
	if(!sortNum){
		art.dialog.tips("请填写角色序号");
		$(obj).attr("disabled" , false);
		return false;
	}
	if(!code){
		art.dialog.tips("请填写角色编号");
		$(obj).attr("disabled" , false);
		return false;
	}else{
		var oricode='[#if Urole??]${Urole.code!}[/#if]';
		if(oricode==''||oricode!=code){
			var issanmecode=true;
			$.ajax({
				url : '/user_permission/validate_code.do',
				type : 'post',
				dataType : 'json',
				async: false,
				data : {code:code},
				success : function(data){
					if(data.success==false){
						art.dialog.tips(data.msg);
						$(obj).attr("disabled" , false);
						issanmecode=false;
					}
				}
			});
			if(issanmecode==false){
				return issanmecode;
			}	
		}
	}
	var selected = $("#functionTree").tree('getChecked');
	if(selected.length == 0){
		art.dialog.tips("请为角色分配权限");
		$(obj).attr("disabled" , false);
		return false;
	}
	var authorityids = [];
	for(var i = 0 ; i < selected.length ; i++){
		authorityids.push(selected[i].id);
	}
	//数据提交
	$.ajax({
		url : '/user_permission/o_addrole.do',
		type : 'post',
		dataType : 'json',
		data : {
			funcids : authorityids , 
			id : roleId ,
			deptId :deptId,
			name : roleName,
			roleType:roleType,
			sortNum:sortNum,
			ifAdmin:ifAdmin,
			code:code
		},
		beforeSend:function(){
            art.dialog.tips("数据提交中...");
        },
		success : function(data){
				if(data.success){
	               location.reload();
				}else{
					error_msg(data.msg , function(){location.reload();});
				}
		}
	});
}

</script>
	<!-- 新增角色菜单 -->
	<div id="createRole"  style="width:300px;" >
	<form  id="createRoleForm" class="form-horizontal" name="formName"  role="form">
	<input type="hidden"  name="id"  id="id" value="[#if Urole??]${Urole.id!}[/#if]"/>
	[#-- 
		<!--   
		<input type="hidden"  name="deptId"  id="deptId" value="${udeptId!}"/>
		-->
	--]
	  <div class="form-group">
	    <label for="roleName" class="col-sm-4 control-label">角色名称</label>
	    <div class="col-sm-6">
	      <input class="form-control" id="roleName" maxlength="200" name="roleName" value="[#if Urole??]${Urole.name!}[/#if]" required />
	    </div>
	  </div>
	   <div class="form-group">
	    <label for="roleType" class="col-sm-4 control-label">角色类型</label>
	    <div class="col-sm-6">
	        <select name="roleType" id="roleType" class="form-control" required>
                <option [#if Urole?? && Urole.roleType==1 ]selected="selected"[/#if] value="1">采购商</option>
                <option [#if Urole?? && Urole.roleType==2 ]selected="selected"[/#if] value="2">供应商</option>
                <option [#if Urole?? && Urole.roleType==3 ]selected="selected"[/#if] value="3">注册用户</option>
            </select>
        </div>
	  </div>
	  <div class="form-group">
	    <label for="ifAdmin" class="col-sm-4 control-label">管理员</label>
	    <div class="col-sm-6">
	       <select name="ifAdmin" id="ifAdmin" class="form-control" required>
                <option [#if Urole?? && Urole.ifAdmin==0 ]selected="selected"[/#if] value="0">否</option>
                <option [#if Urole?? && Urole.ifAdmin==1 ]selected="selected"[/#if] value="1">是</option>
            </select>
        </div>
	  </div>
	   <div class="form-group">
	    <label for="code" class="col-sm-4 control-label">编号</label>
	    <div class="col-sm-6">
	      <input class="form-control" id="code" maxlength="200" name="code" value="[#if Urole??]${Urole.code!}[/#if]" required />
	    </div>
	  </div>
	  <div class="form-group">
	    <label for="sortNum" class="col-sm-4 control-label" >排序</label>
	    <div class="col-sm-6">
	      <input class="form-control" type="number" id="sortNum" maxlength="200" name="sortNum" value="[#if Urole??]${Urole.sortNum!}[/#if]" required />
	    </div>
	  </div>
	  <div class="form-group" style="margin-left: 25px;">
	    <label for="roleFunction" class="form-group_label">角色权限</label>
	      <div  class="form-group_diva" style="max-width: 250px;height:150px;overflow: auto">
	      	<ul id="functionTree">
	      		
	      	</ul>
	      </div>
	  </div>
	  <div class="form-group">
	    <div style="text-align: center;" >
	      <button  onclick="subrole_func(this);" id="role_func" type="button" class="btn btn-info">提交</button>
	    </div>
	  </div>
	</form>
</div>